//package com.czkt.config;
//
//import com.alibaba.fastjson.JSON;
//import com.czkt.util.JWTUtils;
//import jakarta.servlet.FilterChain;
//import jakarta.servlet.ServletException;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.authority.AuthorityUtils;
//import org.springframework.security.core.context.SecurityContext;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StringUtils;
//import org.springframework.web.filter.OncePerRequestFilter;
//
//import java.io.IOException;
//import java.io.PrintWriter;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//@Component
//public class JwtAuthenticationFilter extends OncePerRequestFilter {
//
//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//        response.setContentType("application/json;charset=utf-8");
//        String path = request.getRequestURI();
//        String method = request.getMethod();
//        if ("/login".equals(path) && "POST".equals(method)){
//            filterChain.doFilter(request,response);
//            return;
//        }
//        String token = request.getHeader("Authorization").replace("Bearer","");
//        if (!StringUtils.hasText(token)){
//            Map<String,Object> resultMap = new HashMap<>();
//            resultMap.put("code",401);
//            resultMap.put("msg","您还没有登录呢，请登录后再访问!");
//            PrintWriter out = response.getWriter();
//            out.print(JSON.toJSON(resultMap));
//            out.flush();
//            out.close();
//            return;
//        }
//        if (!JWTUtils.validateToken(token)) {
//            Map<String ,Object > resultMap = new HashMap<>();
//            resultMap.put("code","500");
//            resultMap.put("msg","token已失效");
//            PrintWriter out = response.getWriter();
//            out.print(JSON.toJSON(resultMap));
//            out.flush();
//            out.close();
//            return;
//        }
//        SecurityContext context = SecurityContextHolder.getContext();
//        Map<String ,Object > userInfo = JWTUtils.parseToken(token);
//        Object username = userInfo.get("username");
//        String pwd = "";
//        List<GrantedAuthority> authorityList = AuthorityUtils.createAuthorityList("product:spu:list");
//        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username,new BCryptPasswordEncoder().encode(pwd),authorityList);
//        context.setAuthentication(authenticationToken);
//        filterChain.doFilter(request,response);
//    }
//}